<template>
	<view class="content" :style="{
		'padding-top':height+'px'
	}">
		<view :style="{
		'height':statusBarHeight+'px' 
	}">

		</view>
		<view class="head" :style="{
		'top':statusBarHeight+'px'
	}">
			<view class="head_left">
				<u-icon name="arrow-left" color="#444444" size="24" :bold="true" @click="toBack"></u-icon>
			</view>
			<view class="">
			    编辑讨论
			</view>

		</view>
		<u--form labelPosition="left" labelWidth="200rpx" :model="model1" :rules="rules" ref="uForm">
			<!-- <u-form-item label="星球组别" required prop="userInfo.group" ref="item1" borderBottom v-if="source != 1">
				<view class="picker_fill" @click="show = true">
					<u-icon name="arrow-down-fill"></u-icon>{{model1.userInfo.group}}
				</view>
			</u-form-item> -->
			<u-form-item label="星球标题" required prop="userInfo.title" ref="item2" borderBottom>
				<view class="picker_fill">
					<view style="width: 350rpx;display: flex;
					flex-direction: row-reverse;">
						<u--input v-model="model1.userInfo.title" border="none" :customStyle="{
							'border-radio':'16rpx',
							'margin-top':'16rpx',
							'width':'100%'
						}" placeholder="请输入标题" inputAlign="right"></u--input>
					</view>
				</view>
			</u-form-item>
			<u-form-item labelPosition="top" label="星球内容" required prop="userInfo.content" ref="item2" borderBottom>
				<u--textarea border="none" style="margin-top: 16rpx;" :count="true" maxlength="5000"
					v-model="model1.userInfo.content" placeholder="请输入内容"></u--textarea>
			</u-form-item>
			<view class="props">

				<view class="props_up">
					<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" :maxCount="3">
						<view class="up">
							<image style="width: 48rpx;height: 48rpx;"
								src="@/static/images/icon_cjtl_shangchuantupianhuoshipin.png" mode=""></image>
							<view class="up_text">
								上传图片/视频
							</view>
						</view>

					</u-upload>
				</view>
			</view>
			<view class="fill_text">
				<view class="">
					1、视频文件过大时压缩时间较长，请耐心等待
				</view>
				<view class="">
					2、若出现黑屏，请清理手机内存
				</view>
				<view class="">
					3、建议您调整手机拍摄清晰度选择720p及以下的规格进行拍摄
				</view>
			</view>

			<view class="btn">
				<view class="box_btn" @click="submit">
					提交申请
				</view>
			</view>
		</u--form>
		<u-picker :show="show" :columns="columns" @close="show = false" @cancel="show = false"
			:closeOnClickOverlay="true" @confirm="confirm"></u-picker>

		<floatingBall></floatingBall>
	</view>
</template>

<script>
	import config from "@/config"; // 配置文件
	import storage from "@/common/storage.js"; // 缓存封装
	import {
		createDiscuss,
        discussDetail,
        update_discuss
	} from '@/common/api/start.js'
	export default {
		data() {
			return {
				statusBarHeight: 0,
				height: 0,
				model1: {
					userInfo: {
						group: '请选择', // 星球组别
						title: '',      // 星球标题
						content: ''     // 星球内容
					}
				},
				rules: {
					'userInfo.group': [{
						required: true,
						message: '请选择组别',
					}, {
						validator: (rule, value, callback) => {
							const t = this.model1.userInfo.group == '请选择' ? false : true;
							return t;
						},
						message: '请选择组别',
					}],
					'userInfo.title': {
						type: 'string',
						required: true,
						message: '请选择标题',
						trigger: ['blur', 'change']
					},
					'userInfo.content': {
						type: 'string',
						required: true,
						message: '请输入内容',
						trigger: ['blur', 'change']
					},

				},
				show: false,
				columns: [

				],
				fileList1: [],
				id: -1,
				file_id: [],
				source: 0,
                xinghe_id:0

			}
		},
		created() {
			const statusBarHeight = uni.getSystemInfoSync(); // 获取手机状态栏高度

			this.statusBarHeight = statusBarHeight.statusBarHeight;
			this.$nextTick(() => {
				uni.createSelectorQuery().select('.head').boundingClientRect(data => {
					this.height = data.height;
				}).exec()
			});
		},
		mounted() {
			this.$refs.uForm.setRules(this.rules);
		},
		onLoad(options) {
			if (options && options.id) {
				this.id = options.id;
				console.log('options.source');
				this.source = options.source;
			}

			if (options && options.zu) {
				const zu = options.zu.split(',');
				if (options.shenfen == 0) {
					zu.shift(1);
				}
				this.columns = [zu];
			}
			if (options && options.zu_name) {
				this.model1.userInfo.group = options.zu_name;
			}
            this.getDetail()
		},
		methods: {
			toBack() {
				uni.navigateBack({
					delta: 1
				})
			},
            // 获取帖子详情
            getDetail() {
				discussDetail({
					id: this.id,
				}).then((res) => {
                    this.model1.userInfo.group = res.data.detail.zu
                    this.model1.userInfo.title = res.data.detail.title
                    this.model1.userInfo.content = res.data.detail.content
                    this.file_id = res.data.detail.file_id.split(",")
                    // console.log(this.file_id,9999)
                    this.fileList1 = res.data.detail.common_file
                    this.xinghe_id = res.data.detail.xinghe_id
					// this.datas = res.data.detail;
					// const arr = [
					// 	this.datas.comment_count,
					// 	this.datas.like_count,
					// 	this.datas.gift_count,
					// 	this.datas.collect_count,
					// 	this.datas.gift_count,
					// ];
					// this.list1 = this.list1.map((item, index) => {
					// 	return {
					// 		name: item.name + arr[index],
					// 	};
					// });
					console.log("ressss", res.data.detail);
				});
			},
			confirm(e) {
				console.log('eeee', e);
				this.model1.userInfo.group = e.value[0];
				this.show = false;
			},
			submit() {
                // console.log(this.fileList1)
                // console.log(this.file_id)
                // return 
				this.$refs.uForm.validate().then(res => {
					const file_id = this.file_id.length == 0 ? 0 : this.file_id.join(',');
					update_discuss({
                        xinghe_id:this.xinghe_id,
						id: this.id,
						title: this.model1.userInfo.title,
						// zu_name: this.model1.userInfo.group,
						content: this.model1.userInfo.content,
						file_id
					}).then(res => {
						console.log('ressss', res);
						uni.$u.toast('编辑成功');
						setTimeout(() => {
							uni.navigateBack({
								delta: 1
							})
						}, 800)
					}).catch(err => {
						uni.$u.toast(err);
					})
				}).catch(errors => {
					uni.$u.toast('校验失败')
				})
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
                this.file_id.splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					console.log('resultresult', result);
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result.external_url
					}))
					// this.file_id = result.file_id;
					this.file_id.push(result.file_id);
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				let token = storage.get("token");
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: config.uni_app_web_api_url + '/api/upload/image', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						header: {
							"Access-Token": token
						},
						success: (res) => {
							const data = JSON.parse(res.data);
							console.log('ttttt', res);
							setTimeout(() => {
								resolve(data.data.fileInfo)
							}, 1000)
						}
					});
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	.content {
		width: 100%;
		padding: 0 30rpx;
		box-sizing: border-box;
	}

	.head {
		width: 100%;
		padding: 18rpx 30rpx 47rpx;
		position: fixed;
		display: flex;
		justify-content: center;
		align-items: center;
		box-sizing: border-box;
		z-index: 99;
		box-sizing: border-box;

		.head_left {
			position: absolute;
			left: 30rpx;

		}

		.head_right {
			position: absolute;
			right: 30rpx;
			color: #55B877;
		}

		.text {
			font-size: 36rpx;
			font-family: PingFang SC-Bold, PingFang SC;
			font-weight: 400;
			color: #444444;
		}
	}

	.picker_fill {
		width: 100%;
		display: flex;
		flex-direction: row-reverse;
	}

	.props {
		width: 100%;
		margin-top: 28rpx;
		border-bottom: 1rpx solid #eee;
		padding-bottom: 32rpx;

		.props_text {
			font-size: 24rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			color: #ADADAD;
		}

		.props_up {
			margin-top: 24rpx;

			.up {
				width: 156rpx;
				display: flex;
				flex-direction: column;
				align-items: center;
			}

			.up_text {
				font-size: 24rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				color: #888888;
				margin-top: 12rpx;
			}
		}
	}

	.fill_text {
		width: 100%;
		margin-top: 32rpx;
		font-size: 24rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 400;
		color: #888888;
		line-height: 40rpx;
	}

	.btn {
		width: 100%;
		padding: 0 30rpx 50rpx;
		box-sizing: border-box;
		position: fixed;
		bottom: 0;
		left: 0;

		.box_btn {
			width: 100%;
			height: 96rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			background: linear-gradient(360deg, #50B674 0%, #AEDDBD 100%);
			border-radius: 48rpx;
			font-size: 30rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			color: #FFFFFF;
		}
	}

	/deep/.u-form-item__body__right__message {
		display: flex !important;
		flex-direction: row-reverse !important;
	}
</style>